package ${basepackage}.${projectName}.service.impl;

import ${basepackage}.${projectName}.base.entity.${table.className};
import ${basepackage}.${projectName}.base.utils.CommonValidate;
import ${basepackage}.${projectName}.constant.AppExceptionStatus;
import ${basepackage}.${projectName}.constant.Indicator;
import ${basepackage}.${projectName}.domain.${table.className}Bean;
import ${basepackage}.${projectName}.domain.security.User;
import ${basepackage}.${projectName}.mapper.${table.className}BeanMapper;
import ${basepackage}.${projectName}.service.${table.className}Service;
import cc.hwtech.core.exception.AppException;
import cc.hwtech.core.mybatis.domain.Page;
import cc.hwtech.core.mybatis.domain.PageRequest;
import cc.hwtech.core.util.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>Project: bee-projects</p>
 * <p>Title: ${table.className}ServiceImpl.java</p>
 * <p>Description:   </p>
 * <p>Copyright: Copyright (c) 2016 </p>
 * <p>Company: 华炜云商科技有限公司 www.hwtech.cc</p>
 *
 * @author zh
 * @version 1.0
 * @date 2017-03-09
 */
@Service
public class ${table.className}ServiceImpl implements ${table.className}Service {

    @Autowired
    private ${table.className}BeanMapper ${table.classNameFirstLower}BeanMapper;

    @Override
    public ${table.className}Bean findBeanById(${table.idColumn.simpleJavaType} id, Integer aId) {
        return ${table.classNameFirstLower}BeanMapper.selectBeanById(id, aId);
    }

    @Override
    public ${table.className}Bean findAvailableBeanById(${table.idColumn.simpleJavaType} id, Integer aId) {
        ${table.className}Bean ${table.classNameFirstLower}Bean = null;

        if(CommonValidate.isEmpty(id)){
            throw new AppException(AppExceptionStatus.DATA_NONE_OR_STATUS_ERROR);
        }
        ${table.classNameFirstLower}Bean = this.findBeanById(id, aId);

        if(${table.classNameFirstLower}Bean == null) {
            throw new AppException(AppExceptionStatus.DATA_NONE_OR_STATUS_ERROR);
        }

        return ${table.classNameFirstLower}Bean;
    }

    @Override
    public Page<${table.className}Bean> findBeanPage(PageRequest pageRequest, ${table.className}Bean ${table.classNameFirstLower}Bean, Integer aId) {
        return ${table.classNameFirstLower}BeanMapper.selectBeanPage(pageRequest, ${table.classNameFirstLower}Bean, aId);
    }

    @Override
    public List<${table.className}Bean> findBeanAll(${table.className}Bean ${table.classNameFirstLower}Bean, Integer aId) {
        return ${table.classNameFirstLower}BeanMapper.selectBeanAll(${table.classNameFirstLower}Bean, aId);
    }

    @Override
    @Transactional
    public void doAdd(${table.className}Bean ${table.classNameFirstLower}Bean, User user) {
        this.doValidateForUpOrAdd(${table.classNameFirstLower}Bean, true);
        ${table.className} ${table.classNameFirstLower}Sql = new ${table.className}();
        this.doCopyFromPageToSql(${table.classNameFirstLower}Bean, ${table.classNameFirstLower}Sql);
        //TODO set 新增时，其他字段默认值

        if(true) {
            throw new AppException("请先完成功能");
        }
        //保存到数据库
        ${table.classNameFirstLower}BeanMapper.insert(${table.classNameFirstLower}Sql);
    }

    @Override
    @Transactional
    public void doUpdate(${table.className}Bean ${table.classNameFirstLower}Bean, User user) {
        this.doValidateForUpOrAdd(${table.classNameFirstLower}Bean, false);
        ${table.className} ${table.classNameFirstLower}Sql = this.findBeanById(${table.classNameFirstLower}Bean.get${table.idColumn.columnName}(), null);

        if(${table.classNameFirstLower}Sql == null) {
            throw new AppException(AppExceptionStatus.DATA_NONE);
        }

        this.doCopyFromPageToSql(${table.classNameFirstLower}Bean, ${table.classNameFirstLower}Sql);
        //保存到数据库
        ${table.classNameFirstLower}BeanMapper.updateByPrimaryKey(${table.classNameFirstLower}Sql);
    }

    @Override
    @Transactional
    public void doDel(${table.idColumn.simpleJavaType} id, User user) {
        //TODO 验证删除权限
        ${table.className} ${table.classNameFirstLower} = ${table.classNameFirstLower}BeanMapper.selectByPrimaryKey(id);
        //将状态设置为删除//TODO 请确设置的状态跟数据库状态一直

		if(true) {
			throw new AppException("请先完成功能");
		}

        //${table.classNameFirstLower}.setStatus(Indicator.STATUS_3.getCode());
        ${table.classNameFirstLower}BeanMapper.updateByPrimaryKey(${table.classNameFirstLower});
    }

    @Override
    @Transactional
    public void doDelete(${table.idColumn.simpleJavaType} id, User user) {
        //TODO 验证删除权限
        ${table.classNameFirstLower}BeanMapper.deleteByPrimaryKey(id);
    }

    /**
     * 将页面传来的数据复制到待保存的对象中
     * @param ${table.classNameFirstLower}BeanPage        [必填] 页面传来的对象(跟页面相关)
     * @param ${table.classNameFirstLower}Sql             [必填] 待保存的对象(跟数据库相关)
     */
    private void doCopyFromPageToSql(${table.className}Bean ${table.classNameFirstLower}BeanPage, ${table.className} ${table.classNameFirstLower}Sql) {
        //TODO 页面传值代码
<#list table.columns as column>
<#if (!column.pk)>
        ${table.classNameFirstLower}Sql.set${column.columnName}(${table.classNameFirstLower}BeanPage.get${column.columnName}());
</#if>
</#list>
		if(true) {
			throw new AppException("请先完成功能");
		}
    }

    /**
     * 验证页面传来的对象
     * @param ${table.classNameFirstLower}BeanPage        [必填] 页面传来的对象(跟页面相关)
     * @param isAdd                  [必填] 是否是添加。true=是，当添加验证；false=否，当修改验证
     */
    private void doValidateForUpOrAdd(${table.className}Bean ${table.classNameFirstLower}BeanPage, boolean isAdd) {
        //TODO 验证代码
		if(true) {
            throw new AppException("请先完成功能");
		}
    }
}